/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trirparser;

import java.io.IOException;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 *
 * @author Igor
 */
public class Weather {
    
    private Date date = new Date();
    private DateFormat datum = new SimpleDateFormat("dd.MM.yyyy-HH.mm.ss");
    
    private final String[] mesta = {"Maribor", "Ljubljana", "Celje", "Koper", "Kranj", "Novo+Mesto", "Slovenj+Gradec",
    "Murska+Sobota", "Bled", "Velenje", "Piran", "Portoroz", "Jesenice", "Krsko", "Lendava", "Postojna", "Nova+Gorica"};
    
    private String vrniPodatkeIzStrani(String url) throws MalformedURLException, IOException {
        java.io.InputStream s = null;
        java.io.InputStreamReader r = null;
        StringBuilder content = new StringBuilder();
        try {
            s = (java.io.InputStream)new URL(url).getContent();

            r = new java.io.InputStreamReader(s, "UTF-8");

            char[] buffer = new char[4*1024];
            int n = 0;
            while (n >= 0) {
                n = r.read(buffer, 0, buffer.length);
                if (n > 0) {
                    content.append(buffer, 0, n);
                }
            }
        }
        finally {
            if (r != null) r.close();
            if (s != null) s.close(); 
        }
        return content.toString();
    }
    
    
    public ArrayList<Podatek> vrniPodatkeOMestih() throws MalformedURLException, IOException, ParserConfigurationException, SAXException {
        ArrayList<Podatek> podatki = new ArrayList<Podatek>();
        for(int i = 0; i < mesta.length; i++) {
            String tempPodatki = vrniPodatkeIzStrani("http://www.google.com/ig/api?weather=" + mesta[i] + "+Slovenia+&hl=SL&oe=utf-8");
            //System.out.println(tempPodatki);
            Podatek podatek = vrniUrejenePodatke(tempPodatki, mesta[i]);
            System.out.println(podatek.toString());
            podatki.add(podatek); 
        }
        return podatki;
    }

    private Podatek vrniUrejenePodatke(String tempPodatki, String mesto) throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = factory.newDocumentBuilder();
        InputSource inStream = new InputSource();
        inStream.setCharacterStream(new StringReader(tempPodatki));
        Document doc = db.parse(new InputSource(new StringReader(tempPodatki))); 
        NodeList trenutniPodatki = doc.getDocumentElement().getChildNodes().item(0).getChildNodes().item(1).getChildNodes();
        
        Podatek podatek = new Podatek();
        
        podatek.setDatum(datum.format(date));
        podatek.setMesto(mesto.replace("+", " "));
        podatek.setStanje(trenutniPodatki.item(0).getAttributes().item(0).getNodeValue());
        podatek.setTemperatura(trenutniPodatki.item(2).getAttributes().item(0).getNodeValue());
                
        String tempVlaznost = trenutniPodatki.item(3).getAttributes().item(0).getNodeValue();
        podatek.setVlaznost(tempVlaznost.substring(tempVlaznost.indexOf(": ") + 2, tempVlaznost.indexOf("%")));
        
        podatek.setSlika("http://www.google.com" + trenutniPodatki.item(4).getAttributes().item(0).getNodeValue());
        
        String tempVeter = trenutniPodatki.item(5).getAttributes().item(0).getNodeValue();
        podatek.setSmerVeter(tempVeter.substring(tempVeter.indexOf(": ") + 2, tempVeter.indexOf(" s")));
        podatek.setHitrostVeter(tempVeter.substring(tempVeter.indexOf("hitrostjo ") + 10, tempVeter.indexOf(" km/h")));
        return podatek;
    }
    
    
}
